import React, { useEffect } from 'react'
import { Form, Input, Button, Space, message } from 'antd'
// import Input from 'antd/lib/input/Input'
import { ReqAddHospitalParams } from '../../../api/hospital/hospitalSet/type'
import { addorupdateHopital, reqGetHospitalSetById, reqUpdateHospitalSet } from '@/api/hospital/hospitalSet'
import { useNavigate, useParams, } from 'react-router-dom'
import { useForm } from 'antd/lib/form/Form'

export default function AddorupdateHopital() {
  let navigate = useNavigate()
  // 声明onFinish 函数
  // let onFinish = async (values: ReqAddHospitalParams) => {
  //   // 获得表单的值
  //   // console.log(values)
  //   // 发送请求 新增
  //   await addorupdateHopital(values)
  //   message.success('新增数据成功');
  //   // 跳转到列表页面
  //   navigate('/syt/hospital/hospitalSet')
  // }
  // 返回


  // 把id传过来
  let { id } = useParams()
  // 获取表单元素From
  let [form] = useForm()
  // 修改医院设置
  useEffect(() => {
    let getData = async () => {
      let result = await reqGetHospitalSetById(id as string)
      // console.log(result);
      form.setFieldsValue(result)
    }
    getData()
  }, [])

  let back = () => {
    navigate(-1)
  }
  // 声明onFinishFailed 函数
  let onFinish = async (values: ReqAddHospitalParams) => {
    // 更新医院的设置
    if (!id) {
      await addorupdateHopital(values)
      message.success('新增数据成功')
      back()
      // navigate('/syt/hospital/hospitalSet')

    } else {
      await reqUpdateHospitalSet({
        ...values, id
      })
      message.success('修改成功')
      navigate('/syt/hospital/hospitalSet')
    }
  }

  return (
    <Form
      form={form}
      name="basic"
      labelCol={{ span: 2 }}
      wrapperCol={{ span: 16 }}
      initialValues={{ remember: true }}
      onFinish={onFinish}

      autoComplete="off"
    >
      <Form.Item
        label="医院名称"
        name="hosname"
        rules={[{ required: true, message: '医院名称不能为空!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="医院编号"
        name="hoscode"
        rules={[{ required: true, message: '医院编号不能为空!' }]}
      >
        <Input.Password />
      </Form.Item>

      <Form.Item
        label="API基础路径"
        name="apiUrl"
        rules={[{ required: true, message: 'API基础路径不能为空!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="联系人姓名"
        name="contactsName"
        rules={[{ required: true, message: '联系人姓名不能为空!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="联系人电话"
        name="contactsPhone"
        rules={[{ required: true, message: '联系人电话不能为空!' },
        { pattern: /^1[3456789]\d{9}$/, message: '手机格式不正确' },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item name="remember" valuePropName="checked" wrapperCol={{ offset: 2, span: 16 }}>
      </Form.Item>

      <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
        <Space>
          <Button type="primary" htmlType="submit">
            保存
          </Button>
          <Button type="primary" danger htmlType="submit" onClick={back} >
            返回
          </Button>
        </Space>
      </Form.Item>
    </Form >
  )
}
